package com.byq.mm.gateway;

import java.text.DecimalFormat;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;

public class GraphicsUtil{
	static public Bitmap panelImage(int w, int h){
		Bitmap img = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		Canvas canvas = new Canvas(); 
		canvas.setBitmap(img); 
		GraphicsUtil.drawMeter(canvas, 0, 40, 60, 30.0f, 40.0f, 35);
		GraphicsUtil.drawPanel(canvas, null, 200, 110, 50, 210, 15, 3, null);
		GraphicsUtil.drawPanel(canvas, null, 200, 320, 50, 150, 10, 2, null);
		return img;
	}
	static public void drawPanelValue(Canvas canvas, int[] value, int cx, int cy, int radius, int limit, int step, int sstep, String desc){
		canvas.translate(cx,cy);
		int sr = radius - 10;
		Paint p = new Paint();
		p.setStyle(Paint.Style.STROKE);
		p.setTextAlign(Paint.Align.CENTER);
		p.setAntiAlias(true);
		p.setStrokeWidth(0);
		p.setColor(Color.RED);
		for(double v : value){
			double th = 2* Math.PI * v / limit;
			double vy1 = - 5 * Math.cos(th);
			double vx1 = 5 * Math.sin(th);
			double vy2 = - sr * Math.cos(th);
			double vx2 =  sr * Math.sin(th);
			p.setColor(Color.RED);
			canvas.drawLine((int)vx1, (int)vy1, (int)vx2, (int)vy2, p);
		}
		p.setColor(Color.YELLOW);
		if(value.length == 1)
			canvas.drawText(desc + "��" + format(value[0]),	0 , radius + 40, p);
		else 
			canvas.drawText(desc + "��" + format(value[0]) + "-" +  format(value[1]),0 , radius + 40, p);
		canvas.translate(-cx,-cy);
	
	}
	static public void drawPanel(Canvas canvas, int[] value, int cx, int cy, int radius, int limit, int step, int sstep, String desc){
		Paint p = new Paint();
		p.setStyle(Paint.Style.STROKE);
		p.setTextAlign(Paint.Align.CENTER);
		p.setAntiAlias(true);
		p.setStrokeWidth(3);
		p.setColor(Color.GREEN);
		int lr = radius + 15;
		int sr = radius - 10;
		int ssr = radius - 5;
		canvas.translate(cx,cy);
		canvas.drawCircle(0, 0, 5, p);
		
		canvas.drawCircle(0, 0, radius, p);
		double d = (2 * Math.PI) /limit;
		p.setStrokeWidth(0);
		if(value != null){
			for(double v : value){
				double th = 2* Math.PI * v / limit;
				double vy1 = - 5 * Math.cos(th);
				double vx1 = 5 * Math.sin(th);
				double vy2 = - sr * Math.cos(th);
				double vx2 =  sr * Math.sin(th);
				p.setColor(Color.RED);
				canvas.drawLine((int)vx1, (int)vy1, (int)vx2, (int)vy2, p);
			}
		}
		p.setColor(Color.GREEN);
		for(int i = 0; i< limit; i++){
			double t = i * d;
			double sin = Math.sin(t);
			double cos = Math.cos(t);
			double y1 = - radius * cos;
			double x1 = radius * sin;
			double y2 = - sr * cos;
			double x2 =  sr * sin;
			
			double y3 = - lr * cos;
			double x3 =  lr * sin;
			double y4 = - ssr * cos;
			double x4 =  ssr * sin;
			p.setColor(Color.GREEN);
			if(i%step == 0){
				
				canvas.drawLine((int)x1, (int)y1, (int)x2, (int)y2, p);
				p.setColor(Color.YELLOW);;
				canvas.drawText(format( i), (float)x3, (float)y3, p);
			}else if(i%sstep == 0)
				canvas.drawLine((int)x1, (int)y1, (int)x4, (int)y4, p);
			
			
		}
		if(desc != null &&value != null){
			if(value.length == 1)
				canvas.drawText(desc + "��" + format(value[0]),	0 , radius + 40, p);
			else 
				canvas.drawText(desc + "��" + format(value[0]) + "-" +  format(value[1]),0 , radius + 40, p);
		
		}
		canvas.translate(-cx,-cy);
		
	}
	static public void drawMeterValue(Canvas canvas, double value, int x, int y, float startv, float endv, int pix_unit) {
		Paint p = new Paint();
		p.setAntiAlias(true);
		p.setColor(Color.RED);
		p.setStrokeWidth(3);
		float scope = endv - startv;
		int y1 = y + (int)((scope - value  + startv) * pix_unit);
		int y2 = (int)(pix_unit * scope) + y;
		canvas.drawLine(x, y1, x,y2 , p);
		
		p.setColor(Color.YELLOW);
		canvas.drawText("��ǰ�¶ȣ�" + format(value) + " C",
				x - 10, y - 20, p);
	}

	static public void drawMeter(Canvas canvas, double value, int x, int y, float startv, float endv, int pix_unit) {

	    float scope = endv - startv;
	

	//	

		Paint p = new Paint();
		p.setAntiAlias(true);
		p.setStrokeWidth(5);
		p.setColor(Color.WHITE);
		int y1 = (int)(pix_unit * scope) + y;
		canvas.drawLine(x, y, x, y1, p);
		if(value != 0){
		p.setColor(Color.RED);
		p.setStrokeWidth(3);
		y1 = y + (int)((scope - value  + startv) * pix_unit);
		int y2 = (int)(pix_unit * scope) + y;
		canvas.drawLine(x, y1, x,y2 , p);
		}
		p.setColor(Color.GREEN);
		
		 for(int i = (int)(startv * 10); i <= (int)(endv * 10); i += 2){
			 float v = y + (endv - (float)i/10) * pix_unit;
			if (i% 10 == 0) {
				p.setStrokeWidth(2);

				canvas.drawLine(x + 5, v , x + 20, v, p);
				canvas.drawText(format(i/10) + "",x + 25,  v + 5, p);
				
			} else {
				p.setStrokeWidth(1);

				canvas.drawLine(x + 5, v, x + 12, v, p);
			}
		}
		 if(value != 0){
		p.setColor(Color.YELLOW);
		canvas.drawText("��ǰ�¶ȣ�" + format(value) + " C",
				x - 10, y - 20, p);
		 }

	}

	static String format(double value) {
		DecimalFormat decimalFormat = new DecimalFormat("0.00");

		if (value == (int) value) {
			return (int) value + "";
		} else {
			return decimalFormat.format(value);
		}
	}

}
